package com.spring.task.util;


import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;


/**
 * Date utility
 * 
 * @author zxx
 */

public class DateUtil {

	/**
	 * Date format: "YYYY.MM.DD"
	 */
	public static final String DYYYYMMDD = "DYYYYMMDD";
	/**
	 * Date format: "YYYY.MM"
	 */
	public static final String DYYYYMM = "DYYYYMM";
	/**
	 * Date format: "YY"
	 */
	public static final String YY = "yy";

	/**
	 * Date format: "YYYY"
	 */
	public static final String YYYY = "yyyy";

	/**
	 * Date format: "MM"
	 */
	public static final String MM = "MM";

	/**
	 * Date format: "DD"
	 */
	public static final String DD = "dd";

	/**
	 * Date format: "MM/DD"
	 */
	public static final String MM_DD = "MM/dd";

	/**
	 * Date format: "YYYYMM"
	 */
	public static final String YYYYMM = "yyyyMM";

	public static final String YYYYMMDD = "yyyyMMdd";

	/**
	 * Date format: "YYYY/MM"
	 */
	public static final String YYYY_MM = "yyyy/MM";

	/**
	 * Date format: "YY/MM/DD"
	 */
	public static final String YY_MM_DD = "yy/MM/dd";

	/**
	 * Date format: "YYYY/MM/DD"
	 */
	public static final String YYYY_MM_DD = "yyyy/MM/dd";

	/**
	 * Date format: "YYYY-MM-DD"
	 */
	public static final String OYYYY_MM_DD = "yyyy-MM-dd";

	/**
	 * Date format: "YYYY-MM-DD HH:MI" Add By YEHOOHAHA
	 */
	public static final String OYYYY_MM_DD_HH_MI = "yyyy-MM-dd hh:mm";
	
	/**
	 * Date format: "YYYY-MM-DD HH:MI" Add By YEHOOHAHA
	 */
	public static final String OYYYY_MM_DD_HH2_MI = "yyyy-MM-dd HH:mm";
	/**
	 * Date format: "YYYY-MM-DD HH:MI:SS"
	 */
	public static final String OYYYY_MM_DD_HH2_MI_SS = "yyyy-MM-dd HH:mm:ss";
	/**
	 * Date format: "HH:MI"
	 */
	public static final String HH_MI = "HH:mm";

	/**
	 * Date format: "HHMI"
	 */
	public static final String HHMI = "HHmm";

	/**
	 * Date format: "YY/MM/DD HH:MI"
	 */
	public static final String YY_MM_DD_HH_MI = "yy/MM/dd HH:mm";

	/**
	 * Date format: "YYYY/MM/DD HH:MI"
	 */
	public static final String YYYY_MM_DD_HH_MI = "yyyy/MM/dd HH:mm";
	
	/**
	 * Date format: "YYYY/MM/DD HH:MI:SS"
	 */
	public static final String YYYY_MM_DD_HH_MI_SS = "yyyy/MM/dd HH:mm:ss";

	/**
	 * Date format: "HH:MI:SS"
	 */
	public static final String HH_MI_SS = "HH:mm:ss";

	/**
	 * Date format: "YYYYMMDDHHMISS"
	 */
	public static final String YYYYMMDDHHMI = "yyyyMMddHHmm";

	/**
	 * Date format: "YYYYMMDDHHMISS"
	 */
	public static final String YYYYMMDDHHMISS = "yyyyMMddHHmmss";

	/**
	 * Date format: "YYYYMMDD_HHMISS"
	 */
	public static final String YYYYMMDD_HHMISS = "yyyyMMdd-HHmmss";

	/**
	 * Date format YYYY年MM月DD日
	 */
	public static final String YYYY$MM$DD$ = "yyyy年MM月dd日";

	/**
	 * Date format YYYY年MM月
	 */
	public static final String YYYY$MM$ = "yyyy年MM月";

	/**
	 * Date format MM月DD日
	 */
	public static final String MM$DD$ = "MM月dd日";

	/**
	 * Date format DD日
	 */
	public static final String DD$ = "dd日";

	// modified 2004/06/10 詳細情報修正
	/**
	 * Date format: "ERAYYMMDD"
	 */
	public static final String ERAYYMMDD = "ERAYYMMDD";

	/**
	 * Date format: "ERAYYMM"
	 */
	public static final String ERAYYMM = "ERAYYMM";

	/**
	 * Date format: "HH"
	 */
	public static final String HH = "HH";

	/**
	 * Date format: "MI"
	 */
	public static final String MI = "mm";

	/**
	 * Field value: Year
	 */
	public static final int YEAR = 1;

	/**
	 * Field value: Month
	 */
	public static final int MONTH = 2;

	/**
	 * Field value: Day
	 */
	public static final int DAY_OF_MONTH = 3;

	/**
	 * Field value: Hour
	 */
	public final static int HOUR = 10;

	/**
	 * Field value: Hour of Day
	 */
	public final static int HOUR_OF_DAY = 11;

	/**
	 * Field value: Minute
	 */
	public final static int MINUTE = 12;

	/**
	 * Field value: Second
	 */
	public final static int SECOND = 13;
	
	public static final String YYMMDD = "yyMMdd";

	  

	          

	/**
	 * Get formatted date through custom format
	 * 
	 * @param sFormat
	 *            Format
	 * @param date
	 *            Date which need to be formatted
	 * @return Formatted date
	 */
	public static String getFormatDate(String sFormat, Date date) {
		if (date == null) {
			return "";
		}
		if (DateUtil.YY.equals(sFormat) || DateUtil.YYYY.equals(sFormat)
				|| DateUtil.MM.equals(sFormat) || DateUtil.DD.equals(sFormat)
				|| DateUtil.MM_DD.equals(sFormat)
				|| DateUtil.YYYYMM.equals(sFormat)
				|| DateUtil.YYYYMMDD.equals(sFormat)
				|| DateUtil.YYYY_MM.equals(sFormat)
				|| DateUtil.YY_MM_DD.equals(sFormat)
				|| DateUtil.YYYY_MM_DD.equals(sFormat)
				|| DateUtil.OYYYY_MM_DD.equals(sFormat)
				|| DateUtil.OYYYY_MM_DD_HH_MI.equals(sFormat)
				|| DateUtil.OYYYY_MM_DD_HH2_MI_SS.equals(sFormat)
				|| DateUtil.HH_MI.equals(sFormat)
				|| DateUtil.HHMI.equals(sFormat)
				|| DateUtil.YY_MM_DD_HH_MI.equals(sFormat)
				|| DateUtil.OYYYY_MM_DD_HH2_MI.equals(sFormat)
				|| DateUtil.YYYY_MM_DD_HH_MI_SS.equals(sFormat)
				|| DateUtil.YYYYMMDDHHMISS.equals(sFormat)
				|| DateUtil.YYYY$MM$DD$.equals(sFormat)
				|| DateUtil.YYYY$MM$.equals(sFormat)
				|| DateUtil.MM$DD$.equals(sFormat)
				|| DateUtil.DD$.equals(sFormat) || DateUtil.HH.equals(sFormat)
				|| DateUtil.MI.equals(sFormat)
				|| DateUtil.HH_MI_SS.equals(sFormat)
				|| DateUtil.YYYY_MM_DD_HH_MI.equals(sFormat)
				|| DateUtil.YYMMDD.equals(sFormat)) {
			SimpleDateFormat formatter = new SimpleDateFormat(sFormat);
			return formatter.format(date);
		} else {
			return "";
		}
	}

	public static Date getDate(String sFormat, String date) {
		if (date == null || "".equals(date)) {
			return null;
		}
		if (sFormat == DateUtil.YY || sFormat == DateUtil.YYYY
				|| sFormat == DateUtil.MM || sFormat == DateUtil.DD
				|| sFormat == DateUtil.MM_DD || sFormat == DateUtil.YYYYMM
				|| sFormat == DateUtil.YYYYMMDD || sFormat == DateUtil.YYYY_MM
				|| sFormat == DateUtil.YY_MM_DD
				|| sFormat == DateUtil.YYYY_MM_DD
				|| sFormat == DateUtil.OYYYY_MM_DD || sFormat == DateUtil.HH_MI
				|| sFormat == DateUtil.OYYYY_MM_DD_HH2_MI_SS
				|| sFormat == DateUtil.YY_MM_DD_HH_MI
				|| sFormat == DateUtil.YYYY_MM_DD_HH_MI_SS
				|| sFormat == DateUtil.YYYYMMDDHHMI
				|| sFormat == DateUtil.YYYYMMDDHHMISS
				|| sFormat == DateUtil.YYYYMMDD_HHMISS
				|| sFormat == DateUtil.YYYY$MM$DD$
				|| sFormat == DateUtil.YYYY$MM$ || sFormat == DateUtil.MM$DD$
				|| sFormat == DateUtil.DD$ || sFormat == DateUtil.HH
				|| sFormat == DateUtil.MI) {
			SimpleDateFormat formatter = new SimpleDateFormat(sFormat);
			try {
				return formatter.parse(date);
			} catch (ParseException e) {

				e.printStackTrace();
			}
		}
		return null;
	}

	 
	 
        

	/**
	 * 获取当前天，时分秒均为0
	 * 
	 * @return
	 */
	public static Date getDayBegin(Date date) {
	    if (date == null)
	        return null;
	    Calendar calendar=Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(Calendar.HOUR_OF_DAY,0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 1);
        return calendar.getTime();
	}

	/**
	 * 获取当前天零点，时分秒为23:59:59
	 * 
	 * @return
	 */
	public static Date getDayEnd(Date date) {
	    if (date == null)
            return null;
	    Calendar calendar=Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(Calendar.HOUR_OF_DAY,23);
        calendar.set(Calendar.MINUTE, 59);
        calendar.set(Calendar.SECOND, 59);
        calendar.set(Calendar.MILLISECOND, 999);
        return calendar.getTime();
	}

	public static Date parseDate(String dateValue) {
		if (dateValue == null)
			return null;

		SimpleDateFormat dateFormat = new SimpleDateFormat(
				"yyyy-MM-dd HH:mm:ss");
		Date newDate = null;
		try {
			newDate = dateFormat.parse(dateValue);
		} catch (ParseException pe) {
			newDate = null;
		}
		return newDate;
	}

	 
	
	public static Date getStartTime(Date date){
		Calendar calendar=Calendar.getInstance();
		calendar.setTime(date);
		
		calendar.set(Calendar.HOUR_OF_DAY, 0);
		calendar.set(Calendar.MINUTE, 0);
		calendar.set(Calendar.SECOND, 0);
		return calendar.getTime();
	}
	
	public static Date getEndTime(Date date){
		Calendar calendar=Calendar.getInstance();
		calendar.setTime(date);
		
		calendar.set(Calendar.HOUR_OF_DAY, 23);
		calendar.set(Calendar.MINUTE, 59);
		calendar.set(Calendar.SECOND, 59);
		return calendar.getTime();
	}
	
	
	public static Date getStartTime(Integer year,Integer month,Integer day){
		Calendar calendar=Calendar.getInstance();
		calendar.set(Calendar.YEAR, year);
		calendar.set(Calendar.MONTH, month-1);
		calendar.set(Calendar.DAY_OF_MONTH, day);
		calendar.set(Calendar.HOUR_OF_DAY, 0);
		calendar.set(Calendar.MINUTE, 0);
		calendar.set(Calendar.SECOND, 0);
		return calendar.getTime();
	}
	
	public static Date getEndTime(Integer year,Integer month,Integer day){
		Calendar calendar=Calendar.getInstance();
		calendar.set(Calendar.YEAR, year);
		calendar.set(Calendar.MONTH, month-1);
		calendar.set(Calendar.DAY_OF_MONTH, day);
		calendar.set(Calendar.HOUR_OF_DAY, 23);
		calendar.set(Calendar.MINUTE, 59);
		calendar.set(Calendar.SECOND, 59);
		return calendar.getTime();
	}
 
	/**
	 * 获取时间戳
	 * @param date
	 * @return
	 */
	public static Long getDateToTime(Date date){
		if(date==null){
			return null;
		}
		Long time =date.getTime();
		return time;
	}
	
	/**
	 * 获得日期累加N天
	 * @param date
	 * @return
	 */
	public static Date getDateByAddDay(Date date, int days){
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.add(Calendar.DAY_OF_MONTH, days); 
		//得到累加后日期
		date = calendar.getTime();
		return date;
	}
//	public static void main(String[] args) {
//		Date date =new Date();
//		int days = -30;
//		Date date1 = getDateByAddDay(date,days);
//		System.out.println(date);
//		System.out.println(date1);
//	}
}